//
//  ItemViewController.m
//  mock-store
//
//  Created by aewatson on 10/20/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ItemViewController.h"


@implementation ItemViewController

@synthesize itemName;
@synthesize itemPrice;
@synthesize itemQuantity;
@synthesize itemDescription;
@synthesize item;
@synthesize cart;
@synthesize toolbar;
@synthesize quantityAdd;
@synthesize quantitySubtract;
@synthesize addToCart;
@synthesize editing;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        quantity = 0;       // initialize quantity to 0
        editing = false;    // initialize flag as though current item is new item for cart(not an update of existing quantity)
    }
    return self;
}

-(IBAction)btnQtyAddClicked {
    // add 1, upate textbox
    [self setQuantity:[self getQuantity] + 1];
    itemQuantity.text = [self getQuantityString];
}

-(IBAction)btnQtySubClicked {
    // subtract 1, update textbox
    [self setQuantity:[self getQuantity] - 1];
    itemQuantity.text = [self getQuantityString];
}

-(IBAction)btnAddToCartClicked {
    // add item to cart
    // if currently in update mode for already existing cart item...
    if(editing) {
        // get line item from cart
        LineItem *li;
        // update line item
        [cart updateItemWithSKU:item.itemSKU andQuantity:[NSNumber numberWithInt:quantity]];
        li = [cart getLineItemWithSKU:item.itemSKU];
        NSLog(@"updated quantity: %i", li.quantity);
    } else {
        // otherwise, add item to cart
        if([self getQuantity] > 0) {
            [cart addItem:item andQuantity:quantity];
        }
    }
    // return to prevoius view
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) btnViewCartClicked {
    CartViewController *cartViewController = [[CartViewController alloc] initWithNibName:@"CartViewController" bundle:nil];
    cartViewController.cart = cart;
    
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:cartViewController animated:YES];
    [cartViewController release];
}

-(void)setQuantity: (int)newQuantity {
    if(newQuantity >= 0) quantity = newQuantity;
}

-(int)getQuantity {
    return quantity;
}

-(NSString*)getQuantityString {
    return [NSString stringWithFormat:@"%i", quantity];
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    int myq = quantity;
    myq = quantity;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // set item info
    itemName.text = item.itemName;
    itemPrice.text = [NSString stringWithFormat:@"$%0.2f", [item.itemPrice floatValue]];
    itemDescription.text = item.itemDescription;
    
    // set quantity
    itemQuantity.text = [self getQuantityString];
    
    // If item does not exist in cart
    LineItem *li = [[cart getLineItemWithSKU:item.itemSKU] autorelease];
    if(li == nil) {
        // set editing flag to true and change button label to "Add to Cart"
        editing = false;
        addToCart.titleLabel.text = @"Add to Cart";
    } else {
        // otherwise, set editing to true and change button label to "Update Cart"
        quantity = li.quantity;
        [self setQuantity:[self getQuantity]];
        itemQuantity.text = [self getQuantityString];
        editing = true;
        addToCart.titleLabel.text = @"Update Cart";
    }

    // create toolbar
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    toolbar.frame = CGRectMake(0, 436, 320, 50);    
    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];
    
    // Create button array
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];
        
    // determine if any buttons should appear in the toolbar
    if ([cart getLineItemCount] > 0) {
        
        // create view cart button
        UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:@"View Cart" style:UIBarButtonItemStyleBordered target:self action:@selector(btnViewCartClicked)];
        [buttons addObject:bi];
        [bi release];
        
    }
    toolbar.items = buttons;
    [self.navigationController.view addSubview:toolbar];
}

- (void) viewWillDisappear:(BOOL)animated
{
    toolbar.items = nil;
    int myq = quantity;
    myq = quantity;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
